#include "thread_pool.h"

void tree_dfs(char *path ,int width,int peerfd){
//先打开目录流
    DIR * stream=opendir(path);
    if(!stream){
        error(1,errno,"failed in open %s",path);
    }

    //在打开目录项
    errno=0;
    struct dirent*drip;
    while((drip=readdir(stream))!=NULL){
        //判断忽略。和。。
        if(drip->d_name[0]=='.'){
            continue;
        }
        char sendbuff[1024] = {0};
        //先输出空格，在输出文件名
        for(int i=0;i<width;i++){
            sendbuff[i] = ' ';
        }
        sprintf(sendbuff,"%s%s",sendbuff,drip->d_name);
        int len = strlen(sendbuff);
        //发送到client端
        sendn(peerfd,&len,4);
        sendn(peerfd,sendbuff,len);
        //判断是否目录或是文件，选择是否递归,要拼接成绝对路径
        char fullpath[256];
        sprintf(fullpath,"%s/%s",path,drip->d_name);

        struct stat sb;
        if(stat(fullpath,&sb)==-1){
            printf("%s is not a directory name\n",fullpath);
        }
        if(S_ISDIR(sb.st_mode)){
            tree_dfs(fullpath,width+4,peerfd);
        }else{
        }
    }                                                                                                                                                                                   
    //关闭目录流
    closedir(stream);
}

void treeCommand(task_t * task){
    /* char path[1024] = {0}; */
    /* if(!strcmp(task->data,"\0")){ */
    /*     sprintf(path,"%s/.",cur_cwd); */
    /* }else{ */
    /*     sprintf(path,"%s/%s",cur_cwd,task->data); */
    /* } */
    /* tree_dfs(path,4,task->peerfd); */
    /* int len = 0; */
    /* //给client发送0表示结束 */
    /* sendn(task->peerfd,&len,4); */
}


